package com.nutiteq.location;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;

import com.nutiteq.MapComponent;
import com.nutiteq.components.PlaceIcon;
import com.nutiteq.location.cellid.EricssonLocationApiService;
import com.nutiteq.location.cellid.NokiaCellIdDataReader;
import com.nutiteq.location.cellid.OpenCellIdService;
import com.nutiteq.location.cellid.SonyEricssonCellIdDataReader;
import com.nutiteq.location.providers.CellIdLocationProvider;
import com.nutiteq.location.providers.LocationAPIProvider;

public class LibraryGPSSettings extends Form implements CommandListener {
  // NB! get your own Ericsson API key from https://labs.ericsson.com/apis/mobile-location/
  private static final String ERICSSON_KEY = "Br99vFX69q1qKvVYKfV5u39UwqvaIEKyPfcKHhzj";
  private final Command back;
  private final Command ok;
  private final ChoiceGroup track;
  private final GPSCanvas canvas;
  private final MapComponent mapComponent;
  private final Image gpsImage;
  private final Image connectionLost;
  private final int selectedImplementation;
  private LocationSource source;

  public LibraryGPSSettings(final int selectedImplementation, final MapComponent mapComponet,
      final GPSCanvas canvas, final Image gpsImage, final Image connectionLost) {
    super("Library GPS");
    this.selectedImplementation = selectedImplementation;
    this.mapComponent = mapComponet;
    this.canvas = canvas;
    this.gpsImage = gpsImage;
    this.connectionLost = connectionLost;
    back = new Command("Back", Command.BACK, 0);
    ok = new Command("OK", Command.OK, 0);
    track = new ChoiceGroup("Track GPS", ChoiceGroup.POPUP);

    track.append("Yes", null);
    track.append("No", null);

    addCommand(back);
    addCommand(ok);
    append(track);

    setCommandListener(this);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == back) {
      canvas.show(canvas);
    } else if (cmd == ok) {
      final LocationSource dataSource = getLocationSource(selectedImplementation);
      if (dataSource == null) {
        return;
      }

      canvas.setLocationDataSource(dataSource);
      final LocationMarker marker = new NutiteqLocationMarker(new PlaceIcon(gpsImage, 4, 16),
          new PlaceIcon(connectionLost, 4, 16), 3000, track.getSelectedIndex() == 0);
      dataSource.setLocationMarker(marker);
      mapComponent.setLocationSource(dataSource);
      canvas.addCommand(canvas.removeGPS);
      canvas.show(canvas);
    }
  }

  private LocationSource getLocationSource(final int impl) {
    switch (impl) {
    case GPSCanvas.PROVIDER_LOCATION_API:
      return new LocationAPIProvider(5000);
    case GPSCanvas.PROVIDER_BLUETOOTH:
      return source;
    case GPSCanvas.PROVIDER_SE_CELLID:
      return new CellIdLocationProvider(new SonyEricssonCellIdDataReader(), new EricssonLocationApiService(ERICSSON_KEY));
    case GPSCanvas.PROVIDER_NOKIA_CELLID:
      return new CellIdLocationProvider(new NokiaCellIdDataReader(), new OpenCellIdService());
    case GPSCanvas.PROVIDER_AUTOMATIC_NOKIA_CELLID:
      return new LocationAPIProvider(5000, 1000);
    case GPSCanvas.PROVIDER_NOKIA_ERICSSON:
      return new CellIdLocationProvider(new NokiaCellIdDataReader(), new EricssonLocationApiService(ERICSSON_KEY));
    default:
      return new LocationAPIProvider(5000);
    }
  }

  public void setBluetoothStuff(final LocationSource lSource) {
    source = lSource;
  }
}
